import requests
import re

# 构造请求头
# base_url = r'http://httpbin.org/'
# url = base_url + 'get'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36'
}
# resp = requests.get(url, headers=headers)
#print(resp.text)

# 案例：豆瓣电影
# url = r'https://movie.douban.com/'
# resp = requests.get(url, headers=headers)
#print(resp.status_code)
# print(resp.text)

# 构造 get 请求参数
# 百度案例
# url = r'https://www.baidu.com/s'
# data  = {'wd': '郑州天气'}
# resp = requests.get(url, params=data, headers=headers)
# resp.encoding = 'utf-8'
# part = r'<title>(.*?)</title>'
# title = re.findall(part,resp.text)
#print(title)

# 构造 post 请求的参数 - data
# url = base_url + 'post'
# data = {'param1': 'hello','param2': 'world'}
# resp = requests.post(url, data=data)
#print(resp.text)

# 构造 post 请求的参数 - json
# url = base_url + 'post'
# data = {'param1': 'hello','param2': 'world'}
# resp = requests.post(url, json=data)
#print(resp.text)


# 案例1：百度搜索
url = r'https://www.baidu.com/s'
data  = {'wd': '南阳天气'}
resp = requests.get(url, params=data, headers=headers)
resp.encoding = 'utf-8'
part = r'<title>(.*?)</title>'
title = re.findall(part,resp.text)
print(title)

# 案例2：手机号归属地 http://apis.juhe.cn/mobile/get
url = r'http://apis.juhe.cn/mobile/get'
data = {'phone': '15290301592','key': '。。'}
resp = requests.get(url,params=data)
# print(resp.text)
resp = resp.json()
print(resp)

# 案例3：天气查询 http://apis.juhe.cn/simpleWeather/query 一个get 一个post
url = r'http://apis.juhe.cn/simpleWeather/query'
data = {'city': '郑州','key': '。。。'}
resp = requests.get(url,params=data)
print(resp.text)
# post 方法
url = r'http://apis.juhe.cn/simpleWeather/query'
data = {'city': '郑州','key': '。。。'}
resp = requests.post(url,data=data)
print(resp.text)






